# -*- coding: utf-8 -*-

from django.contrib import admin
from django.db import models

from django.db import models


class BaseModel(models.Model):
    objects = models.Manager()

    # create_time = models.DateTimeField()
    # update_time = models.DateTimeField()

    class Meta:
        abstract = True
        pass


class Portfolio(BaseModel):
    portfolio_code = models.CharField(max_length=8, null=False, blank=False, unique=True, verbose_name='投资组合代码')
    portfolio_name = models.CharField(max_length=64, verbose_name='投资组合名称')

    portfolio_type = models.IntegerField(verbose_name='投资组合类型')
    portfolio_status = models.IntegerField(verbose_name='投资组合状态')

    establish_date = models.DateField(verbose_name='投资组合成立日')
    maturity_date = models.DateField(verbose_name='投资组合到期日')

    def delete(self, using=None, keep_parents=False):
        self.portfolio_status = 0

    def __str__(self):
        return f"Portfolio({self.portfolio_code}: {self.portfolio_name})"

    class Meta:
        db_table = 'port_portfolio'
        verbose_name = '投资组合信息'
        verbose_name_plural = verbose_name
        pass

    pass


# class Product(models.Model):
#     pass
#
#
class Valuation(BaseModel):
    portfolio_code = models.ForeignKey(to=Portfolio,
                                       to_field='portfolio_code',
                                       db_constraint=False,
                                       db_column='portfolio_code',
                                       on_delete=models.DO_NOTHING,
                                       related_name='valuation',
                                       null=True,
                                       blank=True,
                                       verbose_name='投资组合代码'
                                       )

    valuation_date = models.DateField(verbose_name='估值日期')

    class Meta:
        db_table = 'port_valuation'
        verbose_name = '投资组合估值'
        verbose_name_plural = verbose_name
        pass


class Indicator(BaseModel):
    portfolio_code = models.ForeignKey(to=Portfolio,
                                       to_field='portfolio_code',
                                       db_constraint=False,
                                       db_column='portfolio_code',
                                       on_delete=models.DO_NOTHING,
                                       related_name='indicator',
                                       null=True,
                                       blank=True,
                                       verbose_name='投资组合代码'
                                       )

    valuation_date = models.DateField(verbose_name='估值日期')

    # portfolio,stock,bond,fund
    asset_scope = models.CharField(max_length=16, verbose_name='资产范围')

    total_amount = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='总资产')
    net_amount = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='净资产')

    leverage = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='杠杆')
    sharpe_ratio = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='夏普比率')
    modified_duration = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='修正久期')

    YTM = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='到期收益率')
    PVBP = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='基点价值')

    class Meta:
        db_table = 'port_indicator'
        verbose_name = '指标'
        verbose_name_plural = verbose_name
        pass


class Position(BaseModel):
    portfolio_code = models.ForeignKey(to=Portfolio,
                                       to_field='portfolio_code',
                                       on_delete=models.DO_NOTHING,
                                       db_column='portfolio_code',
                                       db_constraint=False,
                                       related_name='position',
                                       null=True,
                                       blank=True,
                                       verbose_name='投资组合代码',
                                       )

    valuation_date = models.DateField(verbose_name='估值日期')

    # 资产代码，合约代码，
    position_code = models.CharField(max_length=8, verbose_name='持仓代码', unique=True)
    position_name = models.CharField(max_length=32, verbose_name='持仓名称')
    position_type = models.CharField(max_length=8, verbose_name='持仓类型')
    # exchange_code = models.CharField(max_length=8, verbose_name='交易所代码')

    quantity = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='数量')
    amount = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='金额')

    market_value = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='市值')

    #
    # total_1d_pnl = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='当日总盈亏')
    # realized_pnl = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='已实现盈亏')
    # floating_pnl = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='未实现盈亏/浮动盈亏')
    #
    # price_pnl = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='价格盈亏')
    # trade_pnl = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='交易盈亏')
    # coupon_pnl = models.DecimalField(max_digits=16, decimal_places=4, verbose_name='利息盈亏')

    class Meta:
        db_table = 'port_position'
        verbose_name = '持仓'
        verbose_name_plural = verbose_name
        pass

    def __str__(self):
        return f"Position({self.position_code}: {self.position_name})"

    pass


# class Analysis(models.Model):
#     portfolio_code = models.ForeignKey(to=Portfolio,
#                                        to_field='portfolio_code',
#                                        on_delete=models.DO_NOTHING,
#                                        db_constraint=False,
#                                        related_name='analysis',
#                                        null=True,
#                                        blank=True,
#                                        verbose_name='投资组合代码'
#                                        )
#
#     class Meta:
#         db_table = 'port_analysis'
#         verbose_name = '分析'
#         verbose_name_plural = verbose_name
#         pass
#
#     pass


# class Performance(models.Model):
#     portfolio_code = models.CharField(max_length=8, verbose_name='')
#
#     class Meta:
#         db_table = 'port_performance'
#         verbose_name = '业绩'
#         verbose_name_plural = verbose_name
#         pass
#
#     pass
#
#
# class Attribution(models.Model):
#     portfolio_code = models.CharField(max_length=8, verbose_name='')
#
#     class Meta:
#         db_table = 'port_attribution'
#         verbose_name = '归因'
#         verbose_name_plural = verbose_name
#         pass
#
#     pass


class AssetInfo(BaseModel):
    asset_code = models.CharField(max_length=8, verbose_name='资产代码')
    asset_name = models.CharField(max_length=8, verbose_name='资产名称')

    class Meta:
        db_table = 'port_asset_info'
        verbose_name = '资产信息-静态数据'
        verbose_name_plural = verbose_name
        pass

    pass


class AssetData(BaseModel):
    # asset_code = models.CharField(max_length=8, verbose_name='资产代码')
    asset_code = models.ForeignKey(to=Position,
                                   to_field='position_code',
                                   on_delete=models.DO_NOTHING,
                                   db_column='asset_code',
                                   db_constraint=False,
                                   related_name='asset_data',
                                   null=True,
                                   blank=True,
                                   verbose_name='资产数据',
                                   )
    asset_date = models.DateField(verbose_name='日期')

    label_base = models.CharField(max_length=8, verbose_name='标签基础', unique=True)

    class Meta:
        db_table = 'port_asset_data'
        verbose_name = '资产数据-时序数据'
        verbose_name_plural = verbose_name
        pass

    pass


class LabelInfo(BaseModel):
    label_code = models.CharField(max_length=8, verbose_name='标签代码')
    label_name = models.CharField(max_length=8, verbose_name='标签名称')

    # label_base = models.CharField(max_length=8, verbose_name='标签基础')
    label_base = models.ForeignKey(to=AssetData,
                                   to_field='label_base',
                                   on_delete=models.DO_NOTHING,
                                   db_column='label_base',
                                   db_constraint=False,
                                   related_name='label_info',
                                   null=True,
                                   blank=True,
                                   verbose_name='标签基础'
                                   )

    label_group = models.CharField(max_length=8, verbose_name='标签分组')
    label_level = models.IntegerField(verbose_name='标签层级')

    start_date = models.DateField(verbose_name='起始日期')
    end_date = models.DateField(verbose_name='截止日期')

    class Meta:
        db_table = 'port_label_info'
        verbose_name = '标签信息-静态数据'
        verbose_name_plural = verbose_name
        pass

    pass


class LabelData(BaseModel):
    label_code = models.CharField(max_length=8, verbose_name='')
    label_base = models.CharField(max_length=8, verbose_name='')
    label_date = models.DateField(verbose_name='日期')

    class Meta:
        db_table = 'port_label_data'
        verbose_name = '标签数据-时序数据'
        verbose_name_plural = verbose_name
        pass

    pass


class LabelLevel(BaseModel):
    label_date = models.DateField(verbose_name='日期')
    # label_base = models.CharField(max_length=8, verbose_name='')
    label_base = models.ForeignKey(to=AssetData,
                                   to_field='label_base',
                                   on_delete=models.DO_NOTHING,
                                   db_column='label_base',
                                   db_constraint=False,
                                   related_name='label_level',
                                   null=True,
                                   blank=True,
                                   verbose_name='标签基础'
                                   )
    label_group = models.CharField(max_length=8, verbose_name='标签分组')
    label_level1 = models.CharField(max_length=8, verbose_name='')
    label_level2 = models.CharField(max_length=8, verbose_name='')
    label_level3 = models.CharField(max_length=8, verbose_name='')
    label_level4 = models.CharField(max_length=8, verbose_name='')
    label_level5 = models.CharField(max_length=8, verbose_name='')
    label_level6 = models.CharField(max_length=8, verbose_name='')
    label_level7 = models.CharField(max_length=8, verbose_name='')
    label_level8 = models.CharField(max_length=8, verbose_name='')
    label_level9 = models.CharField(max_length=8, verbose_name='')

    class Meta:
        db_table = 'port_label_level'
        verbose_name = '标签层级映射-虚拟表，用于SQL替换'
        verbose_name_plural = verbose_name
        pass

    pass
